# _*_ coding:utf-8 _*_
# 开发人员：zhu
# 项目名称：gettest
# 开发时间：2022/3/612:34
# 开发工具：PyCharm
import json
import os

from client.bcosclient import BcosClient
from client.common.compiler import Compiler
from client.datatype_parser import DatatypeParser
from client_config import client_config

if os.path.isfile(client_config.solc_path) or os.path.isfile(client_config.solcjs_path):
    Compiler.compile_file("contracts/HelloWorld.sol")
    # Compiler.compile_file("contracts/SimpleInfo.sol")
abi_file = "contracts/HelloWorld.abi"
data_parser = DatatypeParser()
data_parser.load_abi_file(abi_file)
contract_abi = data_parser.contract_abi


client = BcosClient()
info = client.getinfo()
print("client info:", info)
res = client.getNodeVersion()
print("\n>>---------------------------------------------------------------------")
print("getClientVersion", json.dumps(res, indent=4))
res = client.getBlockNumber()
print("getBlockNumber", json.dumps(res, indent=4))
print("getPeers", client.getPeers())

print("\n>>Deploy:----------------------------------------------------------")
with open("contracts/HelloWorld.bin", 'r') as load_f:
    contract_bin = load_f.read()
    load_f.close()
result = client.deploy(contract_bin)
contract_name = os.path.splitext(os.path.basename(abi_file))[0]
print(contract_name)
to_address = result['contractAddress']  # use new deploy address使用新的部署地址
print(to_address)
# res = client.call(to_address, contract_abi, "getname")
# print("call getname:", res)
